﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.FindArtInfo.Entities;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.FindArtInfo.Models;

namespace BusinessLayer.FindArtInfo
{
    public class ArtistBL
    {
        private readonly DbHelper _db;

        public ArtistBL()
        {
            _db = new DbHelper(new DbConfig().DbFindArtInfoConnectionString);         
        }        

        public List<ArtistModel> Search(string keyWord, int pageIndex, int pageSize)
        {
            var oCommand = new SqlCommand("sp_Artists_Search") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@KeyWord", keyWord));           
            oCommand.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            oCommand.Parameters.Add(new SqlParameter("@PageSize", pageSize));            
            
            var list = _db.GetList<ArtistModel>(oCommand);            
            return list; 
        }

        public void GetCountByKeyWord(string keyWord, out int totalRecord)
        {
            var oCommand = new SqlCommand("sp_Artists_Select_Count_By_KeyWord") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@KeyWord", keyWord));            
            var output = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            _db.ExecuteNonQuery(oCommand);
            totalRecord = (int)output.Value;            
        }

        public ArtistModel GetById(int id)
        {
            var oCommand = new SqlCommand("sp_Artists_Select_By_Id") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            var list = _db.GetList<ArtistModel>(oCommand);
            return list.FirstOrDefault();
        }               

        public List<ArtistLetterModel> GetByLastNameLetter(int type, string letter, int pageIndex, int pageSize)
        {
            var oCommand = new SqlCommand("sp_Artists_Select_By_LastNameLetter") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Type", type));
            oCommand.Parameters.Add(new SqlParameter("@Letter", letter));
            oCommand.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            oCommand.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            var list = _db.GetList<ArtistLetterModel>(oCommand);
            return list;
        }

        public void GetCountByLastNameLetter(int type, string letter, out int totalRecord)
        {
            var oCommand = new SqlCommand("sp_Artists_Select_Count_By_LastNameLetter") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Type", type));
            oCommand.Parameters.Add(new SqlParameter("@Letter", letter));
            var output = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            _db.ExecuteNonQuery(oCommand);
            totalRecord = (int)output.Value;
        }        
    }
}
